﻿using System;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.inside.Manage
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            BindGrid();

            Page.Title = "Quản lý thành viên - " + Configs.WebTitle;
        }

        public void BindGrid()
        {
            var item = Membership.GetAllUsers();
            dyntable_length.InnerHtml = "Tìm thấy <b>" + item.Count + "</b> thành viên.";

            grvUsers.DataSource = item;
            grvUsers.DataBind();
            grvUsers.GridLines = GridLines.None;
        }

        public string ReturnAllRole(object username)
        {
            var strOut = Roles.GetRolesForUser(username.ToString()).Aggregate("", (current, s) => current + (s + ","));
            return strOut.Substring(0, strOut.Length - 1);
        }

        public string ReturnStatus(object isApproved, object isLockedOut)
        {
            var str = "";
            if (bool.Parse(isApproved.ToString()))
            {
                str += "Đang hoạt động";
            }
            else
            {
                str = "Ngừng hoạt động";
            }

            if (bool.Parse(isLockedOut.ToString()))
            {
                str += "&nbsp;<img src='../images/lock_icon.gif' />";
            }
            return str;
        }

        protected void GrvUsersChangePage(object sender, GridViewPageEventArgs e)
        {
            grvUsers.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void GrvUsersRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d9ebfc'");
            //// This will be the back ground color of the GridView Control
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");

            //show disable link
            var lnkStatus = (LinkButton)e.Row.FindControl("lnkStatus");
            var txtApproved = (HiddenField)e.Row.FindControl("txtApproved");

            lnkStatus.Text = bool.Parse(txtApproved.Value) ? "Khóa" : "Mở khóa";

            //return confirm
            var l = (LinkButton)e.Row.FindControl("lnkDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có muốn xóa? ')");
        }

        protected void GrvUsersCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    Membership.DeleteUser(e.CommandArgument.ToString(), true);

                    BindGrid();
                    break;
                case "Status":
                    var user = Membership.GetUser(e.CommandArgument.ToString());
                    if (user != null)
                    {
                        user.IsApproved = !user.IsApproved;
                        Membership.UpdateUser(user);
                    }
                    BindGrid();
                    break;
            }
        }

        protected void GrvUsersDelete(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}